package Synchronized;

public class AccountSync implements Runnable{
    static AccountSync instance=new AccountSync();

    public  static  int i=0;

    @Override
    public void run() {

        //省略其他耗时操作....
        //使用同步代码块，锁对象为instance
//        synchronized (instance){
//            for(int j=0;j<1000000;j++){
//                i++;
//            }
//        }

        for(int j=0;j<1000000;j++){
            synchronized (instance){
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(instance);
        Thread thread1=new Thread(instance);

        thread.start();
        thread1.start();

        thread.join();
        thread1.join();

        System.out.println("i="+i);
        //输出 i=2000000
    }
}
